#ifndef _AvdFILE_H_
#define _AvdFILE_H_

#include "file.h"
#include "folder.h"
#include "filesystemerror.h"

#include <QList>
#include <QString>

namespace Avd {

class Action;

/**
 * Handle .avd/files and .avd/folders.
 */

class AvdFile {

public:
	/**
	 * Simple constructor.
	 * @param absolutePath absolute filesystem path
	 * @param path relative path
	 */
	AvdFile(const QString& absolutePath, const QString& path) throw(FileSystemError);

	/**
	 * Check files and folders.
	 * @return a list of actions to sync.
	 */
	QList<Action*> check() const;

	/**
	 * Check local files and folders with server's files and folders.
	 * @return a list of actions to sync.
	 */
	QList<Action*> update(QList<File> files, QList<Folder> folders) const;

	/**
	 * Update or append the line on .avd/files referencing \a file.
	 * @param file
	 */
	void add(const File& file) const throw(FileSystemError);

	/**
	 * Update or append the line on .avd/folders referencing \a folder.
	 * @param folder
	 */
	void add(const Folder& folder) const throw(FileSystemError);

	/**
	 * Remove the line on .avd/files referencing \a file.
	 * @param file
	 */
	void remove(const File& file) const throw(FileSystemError);

	/**
	 * Remove the line on .avd/folders referencing \a folder.
	 * @param folder
	 */
	void remove(const Folder& folder) const throw(FileSystemError);

private:
	QList<Action*> checkFiles() const;
	QList<Action*> checkFolders() const;
	QList<Action*> updateFiles(const QList<File> &files) const;
	QList<Action*> updateFolders(const QList<Folder> &folders) const;

	/**
	 * Helper function. Read a \a line an generate a File object.
	 * @param line
	 */
	File lineToFile(const QString &line) const;

	/**
	 * Helper function. Read a \a line an generate a Folder object.
	 * @param line
	 */
	Folder lineToFolder(const QString &line) const;

	QString absolutePath;
	QString path;
};

}

#endif // _AvdFILE_H_
